package priv.wangy.micro.common.po.valid.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import priv.wangy.micro.common.po.valid.IntValues;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author 张嘉琪
 * @since 2023-04-09
 */
public class IntValuesValidator implements ConstraintValidator<IntValues, Integer> {

    private int[] values;

    @Override
    public void initialize(IntValues constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        values = constraintAnnotation.values();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return Objects.nonNull(value) && Arrays.stream(values).anyMatch(i -> i == value);
    }
}
